/*
 * Checker.cpp
 *
 *  Created on: 19.10.2012
 *      Author: ddreval
 */

#include "Checker.h"

using namespace Osp::Base;
using namespace Osp::Base::Runtime;
using namespace Osp::Net::Http;
using namespace Osp::Base::Utility;

Checker::Checker(HttpSession* pSession, String pHostAddr) {
	String* pProxyAddr = null;
	session = new HttpSession();
	hostAddr = pHostAddr;
	session->Construct(NET_HTTP_SESSION_MODE_NORMAL, pProxyAddr, hostAddr, null);
	Construct();
	Start();
	SendUserEvent(1, null);
}

Checker::Checker() {
	AppLog("Empty checker constructor");
}

Checker::~Checker() {
	AppLog("Destroying checker");
}

result
Checker::Construct(void) {
	result r = E_SUCCESS;
	r = Thread::Construct(THREAD_TYPE_EVENT_DRIVEN);
	AppLog("Checker start result %s", GetErrorMessage(r));
	return r;
}

bool
Checker::OnStart(void) {
	AppLog("Checker OnStarts");
	return true;
}

void
Checker::OnStop(void) {
	AppLog("Checker On Stop");
}

void
Checker::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList *pArgs) {

	AppLog("OnUserEventReceivedN");

	Thread::GetCurrentThread()->Sleep(2000);

	result r = E_SUCCESS;

	//String uri = L"http://192.168.0.184:8080/beholder-master/check";
	String uri(hostAddr);
	uri.Append("/beholder-master/check");
	HttpTransaction* pHttpTransaction = null;

	// Open a new Http Transaction within the HttpSession
	pHttpTransaction = session->OpenTransactionN();

	// Add Listener
	// OnTransactionCompleted() will be called when response is arrived
	pHttpTransaction->AddHttpTransactionListener(*this);

	// Get Http Request
	HttpRequest* pHttpRequest = pHttpTransaction->GetRequest();

	// Set HTTP Method
	pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);

	// Set Uri
	pHttpRequest->SetUri(uri);

	// Submit a request with chunked mode
	r = pHttpTransaction->Submit();

	AppLog("Submit result: %S", GetErrorMessage(r));

}

void
Checker::OnTransactionAborted (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction, result r) {
	AppLog("Transaction Aborted");
}

void
Checker::OnTransactionCertVerificationRequiredN (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction, Osp::Base::String *pCert) {
	AppLog("OnTransactionCertVerificationRequiredN");
}

void
Checker::OnTransactionCompleted (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction) {
	AppLog("OnTransactionCompleted");
	delete &httpTransaction;
	SendUserEvent(1, null);
}

void
Checker::OnTransactionHeaderCompleted (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction, int headerLen, bool bAuthRequired) {
	AppLog("OnTransactionHeaderCompleted");
}

void
Checker::OnTransactionReadyToRead (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction, int availableBodyLen) {
	AppLog("OnTransactionReadyToRead");
}

void
Checker::OnTransactionReadyToWrite (Osp::Net::Http::HttpSession &httpSession, Osp::Net::Http::HttpTransaction &httpTransaction, int recommendedChunkSize) {
	AppLog("OnTransactionReadyToWrite");
}
